package com.mustafaiev.tair.cts.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.mustafaiev.tair.constant.Attributes;
import com.mustafaiev.tair.constant.ViewNames;
import com.mustafaiev.tair.cts.dto.PayerDTO;
import com.mustafaiev.tair.cts.exeption.DataNotRetrievedException;
import com.mustafaiev.tair.cts.service.PayerService;
import com.mustafaiev.tair.cts.validation.PayerValidator;

@Controller
public class PayerController {

	@Autowired
	private PayerService payerService;

	@InitBinder(value = { "payer" })
	protected void payersInitBinder(final WebDataBinder binder) {
		binder.setValidator(new PayerValidator(this.payerService));
	}

	@RequestMapping(value = "/payers", method = RequestMethod.GET)
	public ModelAndView goToPayersPage() throws DataNotRetrievedException {
		final ModelAndView view = new ModelAndView("payers");
		final List<PayerDTO> payers = this.payerService.getPayers();
		view.addObject("payers", payers);
		return view;
	}

	@RequestMapping(value = "/payer/add", method = RequestMethod.GET)
	public ModelAndView goToAddPayerPage() {
		final ModelAndView view = new ModelAndView("add_payer");
		return view;
	}

	@RequestMapping(value = "/payer/save", method = RequestMethod.POST)
	public String savePayer(
			@Valid @ModelAttribute("payer") final PayerDTO payerDto,
			final BindingResult binder, final HttpServletRequest request) {
		this.payerService.savePayer(payerDto);
		return "redirect:/payers";
	}

	@RequestMapping(value = "/payer/edit", method = RequestMethod.POST)
	public String editPayer(@RequestParam(value = "id") final Long id,
			@RequestParam(value = "firstname") final String firstname,
			@RequestParam(value = "lastname") final String lastname,
			@RequestParam(value = "email") final String email,
			@RequestParam(value = "password") final String password) {

		final PayerDTO payer = new PayerDTO();
		payer.setFirstname(firstname);
		payer.setEmail(email);
		payer.setLastname(lastname);
		payer.setPassword(password);

		this.payerService.savePayer(payer);
		return "redirect:/payers";
	}

	@RequestMapping(value = "/payer/{payerId}/edit", method = RequestMethod.GET)
	public ModelAndView editPayer(
			@PathVariable(value = "payerId") final Long payerId)
			throws DataNotRetrievedException {
		final ModelAndView view = new ModelAndView(ViewNames.ADD_PAYER);
		final PayerDTO payerDTO = this.payerService.getPayer(payerId);
		view.addObject(Attributes.PAYER, payerDTO);
		view.addObject(Attributes.IS_EDIT, true);
		return view;
	}

	@RequestMapping(value = "/payer/{payerId}", method = RequestMethod.GET)
	public ModelAndView viewPayer(
			@PathVariable(value = "payerId") final Long payerId)
			throws DataNotRetrievedException {
		final ModelAndView view = new ModelAndView(ViewNames.VIEW_PAYER);
		final PayerDTO payerDTO = this.payerService.getPayer(payerId);
		view.addObject(Attributes.PAYER, payerDTO);
		return view;
	}
}
